package com.example.myblog.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.myblog.entity.MCategory;
import com.example.myblog.service.MCategoryService;
import com.example.myblog.service.MPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletConfigAware;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import java.util.List;


/**
 * 查询m_category表，在header-panel中展示
 * ContextStartup类会在项目启动时执行run方法(因为继承了ApplicationRunner)
 */
@Component
public class ContextStartup implements ApplicationRunner/*, ServletContextAware*/ {

    @Autowired
    MCategoryService categoryService;

    //自动注入就行，不必实现ServletContextAware接口这么麻烦
    @Autowired
    ServletContext servletContext;

    @Autowired
    MPostService postService;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        List<MCategory> categories = categoryService.list(new QueryWrapper<MCategory>()
                .eq("status", 0)
        );
        servletContext.setAttribute("categories",categories);
        //初始化本周热议数据，存入redis
        postService.initWeekRank();
    }


//    @Override
//    public void setServletContext(ServletContext servletContext) {
//        this.servletContext = servletContext;
//    }
}
